package net.alan.modules.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.alan.common.utils.DateUtil;
import net.alan.modules.bean.*;
import net.alan.modules.cache.WebContextUtil;
import net.alan.modules.handle.BaseExecption;
import net.alan.modules.service.IProjectService;
import net.alan.modules.service.ITaskService;
import net.alan.modules.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Api(tags = "任务管理")
@Controller
@RequestMapping(value = "task")
public class TaskController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
    @Autowired
    private ITaskService taskService;
    @Autowired
    private IUserService userService;
    @Autowired
    private CustomConfig customConfig;
    @Autowired
    private IProjectService projectService;

    @ApiOperation(value = "添加任务", notes = "")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(Task task, String time) throws Exception {
        try {
            Project project = this.projectService.getById(Project.class, task.getProjectId());
            if (WebContextUtil.getUserId() != project.getUser().getId()) {
                return ResultUtil.error("只有项目创建者可以添加任务");
            }
            task.setEndDate(DateUtil.formatDate(time));
            task.setFromUserId(WebContextUtil.getUserId());
            taskService.saveTask(task);
        } catch (Exception baseExecption) {
            return ResultUtil.noRole();
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "添加任务评论", notes = "")
    @RequestMapping(value = "addcomment", method = RequestMethod.POST)
    @ResponseBody
    public Result addcomment(TaskComment taskComment, Integer toUserId) throws Exception {
        try {
            String content = taskComment.getContent();
            if (content.startsWith("@")) {
                content = content.substring(content.indexOf(" ") + 1, content.length());
                taskComment.setContent(content);
            }
            taskComment.setFromUser(WebContextUtil.getCurrentUser().getUser());
            if (toUserId != null && toUserId > 0) {
                User user = userService.getById(User.class, toUserId);
                taskComment.setToUser(user);
            }
            taskService.save(TaskComment.class, taskComment);
        } catch (BaseExecption baseExecption) {
            return ResultUtil.error("保存异常");
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除任务", notes = "")
    @ApiImplicitParam(name = "id", value = "任务ID", required = true, dataType = "Integer")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") Integer id) throws Exception {
        return taskService.deleteTask(id);
    }

    @ApiOperation(value = "结束任务", notes = "")
    @ApiImplicitParam(name = "id", value = "任务ID", required = true, dataType = "Integer")
    @RequestMapping(value = "end/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result end(@PathVariable("id") Integer id) throws Exception {
        return taskService.endTask(id);
    }

    @ApiOperation(value = "删除任务评论", notes = "")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @RequestMapping(value = "deletecomment/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result deletecomment(@PathVariable("id") Integer id) throws Exception {
        try {
            return taskService.delete(TaskComment.class, id);
        } catch (BaseExecption baseExecption) {
            baseExecption.printStackTrace();
        }
        return ResultUtil.error("网络异常");
    }

    @ApiOperation(value = "获取单个任务", notes = "")
    @ApiImplicitParam(name = "id", value = "Id", required = true, dataType = "Integer")
    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Task get(@PathVariable("id") Integer id) throws Exception {
        return taskService.getById(Task.class, id).copy();
    }

    @ApiOperation(value = "任务列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public PageModel list(Integer projectId, PageModel pageModel) throws Exception {
        Page<Task> list = this.taskService.findTask(WebContextUtil.getUserId(), projectId, pageModel);
        return pageModel.toPageModel(list);
    }

    @ApiOperation(value = "任务评论列表")
    @RequestMapping(value = "listcomment", method = RequestMethod.GET)
    @ResponseBody
    public PageModel listcomment(Integer taskId, PageModel pageModel) throws Exception {
        Page<TaskComment> list = this.taskService.findTaskCommentByTaskId(taskId, pageModel);
        return pageModel.toPageModel(list);
    }

    @ApiOperation(value = "任务附件列表")
    @RequestMapping(value = "listattach", method = RequestMethod.GET)
    @ResponseBody
    public PageModel listattach(Integer taskId, PageModel pageModel) throws Exception {
        Page<ProjectAttach> list = this.projectService.findProjectAttachByTaskId(taskId, pageModel);
        return pageModel.toPageModel(list);
    }

    @ApiOperation(value = "任务日志列表")
    @RequestMapping(value = "listlog", method = RequestMethod.GET)
    @ResponseBody
    public PageModel listlog(Integer taskId, PageModel pageModel) throws Exception {
        Page<ProjectLog> list = this.projectService.findProjectLogByTaskId(taskId, pageModel);
        return pageModel.toPageModel(list);
    }


}
